package cn.hallele.leinsteining.ms.support.starter.components.controller.aspect.children;

import cn.hallele.leinsteining.mc.infrastructure.metadata.constants.MsgCodeConst;
import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Response;
import cn.hallele.leinsteining.ms.infrastructure.util.network.HttpUtil;
import cn.hallele.leinsteining.ms.support.starter.components.controller.aspect.BaseExceptionAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 兜底的异常处理
 *
 * @author anle5
 * @since 2024/7/23 23:37
 */
@Slf4j
@RestControllerAdvice
public class BottomExceptionAspect extends BaseExceptionAspect {
    /**
     * 拦截所有 exception 的报错
     *
     * @param e {@link Exception}
     * @return 错误响应
     */
    @ExceptionHandler(Exception.class)
    public Response<?> exception(Exception e) {
        String msgCode = MsgCodeConst.FAILED;
        String msgDesc = i18n.getMessage(msgCode, new Object[]{}, HttpUtil.getLocale());
        log.error("Find an Exception. MsgCode: {}, msgDesc: {}.", msgCode, msgDesc, e);
        return buildResponse(msgCode, msgDesc);
    }

    /**
     * 拦截所有 throwable 的报错
     *
     * @param e {@link Throwable}
     * @return 错误响应
     */
    @ExceptionHandler(Throwable.class)
    public Response<?> throwable(Throwable e) {
        String msgCode = MsgCodeConst.FAILED;
        String msgDesc = i18n.getMessage(msgCode, new Object[]{}, HttpUtil.getLocale());
        log.error("Find an Throwable. MsgCode: {}, msgDesc: {}.", msgCode, msgDesc, e);
        return buildResponse(msgCode, msgDesc);
    }
}
